package cn.otrue.patienthealthmanager.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.ImageStream;

public class SaleGoodsImageAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<ImageStream> images;
	private Context context;
	private SaleGoodsImageAdapter adapter;

	public SaleGoodsImageAdapter(Context context, List<ImageStream> images) {
		inflater = LayoutInflater.from(context);
		this.images = images;
		this.context = context;
		adapter = this;
	}

	@Override
	public int getCount() {
		if (images == null) {
			return 1;
		}
		return images.size() > 0 ? images.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	private int cancaeOrtrue = 0;
	private int check;
	private String cmseq;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		convertView = inflater.inflate(R.layout.activity_sale_addgoods_image,
				null);
		ImageView image = (ImageView) convertView
				.findViewById(R.id.sale_goods_items);
		image.setImageBitmap(images.get(position).bitmap);
	
		
		TextView delete = (TextView) convertView
				.findViewById(R.id.sale_goods_delete);
		CheckBox sale_goods_mainimage = (CheckBox) convertView
				.findViewById(R.id.sale_goods_mainimage);
		if (position == images.size() - 1) {
			delete.setVisibility(View.GONE);
			sale_goods_mainimage.setVisibility(View.GONE);
		}
		if ("1".equals(images.get(position).mainimg)) {
			sale_goods_mainimage.setChecked(true);
			check = position;
		}
		sale_goods_mainimage
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							images.get(check).mainimg = "0";
							images.get(position).mainimg = "1";
							adapter.notifyDataSetChanged();
						}
					}
				});
		delete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				cancaeOrtrue = position;
				if ("net".equals(images.get(position).from)) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							context);
					builder.setPositiveButton("确定", listener);
					builder.setNegativeButton("取消", listener);
					builder.setTitle("确定删除");
					cmseq=images.get(position).cmseq;
					builder.create().show();
				} else {
					images.remove(position);
				}
				adapter.notifyDataSetChanged();
			}
		});
		return convertView;
	}

	private DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			if (which== DialogInterface.BUTTON_POSITIVE) {
				images.remove(cancaeOrtrue);
				adapter.notifyDataSetChanged();
			}
		}
	};

}
